<?php
namespace app\admin\controller;
use think\Db;//导入数据库类 (DB类)
class Role extends Common
{


	//管理员设置
	public function index()
    {  
// 	    $subQuery=Db::view('Role')->buildSql();
	   
// 	    $count=Db::table($subQuery.'a')->count();
	    
// 	    $pagesize='3';
// 	    $page=new \util\Page($count,$pagesize);
// 	    $page_u=$page->fpage();
// 	    $page=input('get.page');
// 	    $page=isset($page)?$page:"1";
    	$data=db('role')->paginate(2);
    	$page_u=$data->render();
    	$this->assign('data',$data);
    	$this->assign('page',$page_u);
       	return view('Jurisdiction/job');
        
    }

   public function edit($id)
   {
    
    $data=db('role')->where('id',$id)->find();
    $this->assign('data',$data);
    return view('Jurisdiction/roleedit');
   }

   public function doedit()
   {
    
  if (empty(input('role_name')&&input('role_intro'))) {
            $this->error('输入信息有误');
        }
        $data = ['role_name' =>input('role_name'), 'role_intro' => input('role_intro'),'status'=>input('status')];
        $rus=db('role')->where('id',input('id'))->update($data);
        if ($rus>0) {
            $this->success('修改成功','admin/role/index');
        }else{
            $this->error('错误');
        }
   }


    public function del($id)
    {
        $data=db('role')->where('id',$id)->delete();
        if ($data>0) {
            $this->success('删除成功');
        }else{
            $this->error('错误');
        }
    }



    public function add()
    {

      return view('Jurisdiction/roleadd');
    }



     public function doadd()
    {

      if (empty(input('role_name')&&input('role_intro'))) {
        $this->error('输入信息有误');
      }
      $status=input('post.status');
      $data = ['role_name' =>input('role_name'), 'role_intro' => input('role_intro'),'status'=>$status];
      $res=db('role')->insert($data);
      if ($res>0) {
        $this->success('添加成功','admin/role/index');
      }else{
        $this->error('添加失败');
      }
    }


    //分配权限
    
    public function rolelist($id)
    {
      
      $data=db('method')->select();
     
      $list=db('role')->where('id',$id)->find();
      //查询职位拥有的权限
     
      $ro_node =db('role_method')->where('role_id',$list['id'])->select();
      // dump($ro_node);exit;
        $ro_nodes = array();
        //遍历重组数组
        foreach($ro_node as $v){
            $ro_nodes[] = $v['node_id'];
        }
        // var_dump($ro_nodes);exit;
      $this->assign('data',$data);
      $this->assign('list',$list);
      $this->assign('ro_nodes',$ro_nodes);
      return view('Jurisdiction/nodelist');
    }



  //为角色添加权限
  public function savenode($id){
  
    if(empty($_POST['node'])){
            $this->error("必须选择一个节点！");
        }

    //删除该 角色的 所有信息--避免重复添加
    db('role_method')->where('role_id',$id)->delete();
    foreach($_POST['node'] as $v){
            $data['node_id'] = $v;
            $data['role_id'] =$id;
            //执行添加
            db('role_method')->insert($data);
        }
     $this->success('添加成功','admin/role/index');
  }

   
}



	
